<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" :model="formInline" label-position="right">
      <el-form-item label="考勤时间">
        <el-date-picker v-model="formInline.clockMonth" type="month" placeholder="选择月"
        format="yyyy-MM" value-format="yyyy-MM" @change="onSubmit"></el-date-picker>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading"><!-- :span-method="objectSpanMethod" -->
          <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
          <el-table-column prop="clockDate" align="center" label="日期" show-overflow-tooltip></el-table-column>
          <el-table-column prop="punchInType" align="center" label="打卡类型" show-overflow-tooltip></el-table-column>
          <el-table-column prop="clockTime" align="center" label="打卡时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="punchInAddress" align="center" label="打卡地点" show-overflow-tooltip></el-table-column>
          <el-table-column prop="punchInStatus" align="center" label="出勤类型" show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.fieldPunchIn==1?'外勤':'内勤'}}
            </template>
          </el-table-column>
          <el-table-column prop="punchInStatus" align="center" label="打卡状态" show-overflow-tooltip></el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { AttendanceStatistics } from '@/api/attenDances.js';
export default {
  name: 'AttendanceRecord',
  props: {
    clockMonth: {
      default: undefined
    },
    userId: {
      default: undefined
    }
  },
  data () {
    return {
      formInline: {
        clockMonth: null,
        userId: null
      },
      tableData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      times: ''
    }
  },
  created () {
  },
  computed: {
    isEdit () {
      return this.userId != null && this.userId !== '';
    }
  },
  mounted () {
    if (this.isEdit) {
      this.formInline.userId = this.userId
      this.formInline.clockMonth = this.clockMonth
      this.loadListWidgetData()
    }
  },
  methods: {
    /**
     * 考勤统计
     */
    loadListWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        AttendanceStatistics.recordlist(this, params).then(res => {
          let listData = res.data.records
          this.tableData = listData
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1
      this.loadListWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadListWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadListWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadListWidgetData()
    }
  }
}
</script>
